package com.kingtone.rongji.fragment;

import java.util.ArrayList;
import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.kingtone.rongji.MainActivity;
import com.kingtone.rongji.R;
import com.kingtone.rongji.adapter.MyCommentsAdapter;
import com.kingtone.rongji.biz.Comments;
import com.kingtone.rongji.tool.AppContasts;
import com.kingtone.rongji.tool.RongJiRestClient;
import com.kingtone.rongji.tool.RongJiUtils;
import com.kingtone.rongji.tool.SharedPreferencesUtil;
import com.loopj.android.http.AsyncHttpResponseHandler;
public class MyCommentsFragment extends BaseFragment {

	private TextView title = null;
	private ImageView backBtn = null;
	private ImageView leftIcon = null;
	private TextView rightTitle = null;
	private ListView commentsList = null;
	private MainActivity mActivity;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mActivity = (MainActivity) this.getActivity();
		getReviewList();
	}
	
	public View onCreateView(LayoutInflater paramLayoutInflater, ViewGroup paramViewGroup, Bundle paramBundle) {
		View localView = paramLayoutInflater.inflate(R.layout.fragment_my_comments, paramViewGroup, false);
		setupView(localView);
		return localView;
	}
	
	private void setupView(View paramView)
	{
		title = (TextView)paramView.findViewById(R.id.title);
		title.setText(getString(R.string.my_comments));
		backBtn = (ImageView)paramView.findViewById(R.id.back);
		backBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) 
			{
				getActivity().onBackPressed();
			}
		});
		
		leftIcon = (ImageView)paramView.findViewById(R.id.left_icon);
		leftIcon.setBackgroundResource(R.drawable.icon_ping);
		
		rightTitle = (TextView)paramView.findViewById(R.id.right_title);
		rightTitle.setText(getString(R.string.my_comments));
		
		commentsList = (ListView)paramView.findViewById(R.id.my_comments_list);
		//点击打开产品详情
		commentsList.setOnItemClickListener(new OnItemClickListener()
		{
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id)
			{
			}
		});
	}
	
	/**
	 * 获取评论列表
	 */
	public void getReviewList()
	{
		String userId = SharedPreferencesUtil.getInstance(mActivity).getString(AppContasts.USERID_KEY);
		RongJiRestClient.getReviewList(userId, 0, 1000,new AsyncHttpResponseHandler() {

			@Override
			public void onStart() {
				RongJiUtils.showEmptyProgress(getActivity());
			}
			
			@Override
			public void onFailure(int arg0, Header[] arg1, byte[] arg2,
					Throwable arg3) {
			    if(arg2 != null)
			    {
			    	String filureStr = new String(arg2);
					Log.d("failure", filureStr);
			    }
				RongJiUtils.dismissProgress();
			}

			@Override
			public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
				
				RongJiUtils.dismissProgress();
				
				if(arg2 != null && !new String(arg2).equalsIgnoreCase("null"))
				{
					String result = new String(arg2);
					JSONObject obj = null;
					ArrayList<Comments> list = new ArrayList<Comments>();
					try {
						obj = new JSONObject(result);
						if(obj.has("Data"))
						{
							JSONArray pArray = obj.getJSONArray("Data");
							Gson gson = new Gson();
							JsonParser parser = new JsonParser();
							JsonElement element = parser.parse(pArray.toString());
							if(element instanceof JsonArray)
							{
								JsonArray array = element.getAsJsonArray();
								for(JsonElement myComments:array) {
									Comments object = gson.fromJson(myComments, Comments.class);
									list.add(object);
								}
							}
							
							MyCommentsAdapter addAdapter = new MyCommentsAdapter(mActivity, list);
							commentsList.setAdapter(addAdapter);
						}
					} catch (JSONException e) {

						e.printStackTrace();
					}
				}
			}	
		});
	}
}
